﻿/* ------------------------------------------------------------------------
Class: Google Maps
Use: Hooyoo team
Author: Swing 
Version: 1.0
------------------------------------------------------------------------- */

(function($) {

    $.fn.googleMaps = function(settings) {
        settings = jQuery.extend({
            model: 'show', /* mark/show*/
            zoom: 13,
            address: null,
            onSetPoint: function(point) { },
            callback: function() { } /* callback called when closing the popin */
        }, settings);
        $.googleMaps = {};

        $.googleMaps.focusByAddress = function(containerId, address) {
            google.load("maps", "2", { "callback": function() {
                var container = $('#' + containerId);
                if (container.height() != 300) container.hide().height(300).slideDown('normal').scrollTop();
                var map = new google.maps.Map2(container.get(0));
                _focusByAddress(map, address);

            }
            });
        };
        var $elms = this;
        if (!settings.point) settings.point = '0,0';
        var points = settings.point.split(',');
        settings.x = points[0]; settings.y = points[1];
        google.load("maps", "2", { "callback": function() {
            //begin
            return $elms.each(function() {


                if (!settings.address && settings.point == '0,0')
                    $(this).html('未找到位置信息..');
                else {
                    // loading google maps

                    $(this).hide().height(300).slideDown('normal');
                    var map = new google.maps.Map2(this);
                    GEvent.addListener(map, "load", settings.callback);
                    map.addControl(new GLargeMapControl());

                    if (settings.point != '0,0') {
                        var point = new google.maps.LatLng(settings.x, settings.y);
                        _focus(map, point);
                    }
                    else
                        _focusByAddress(map, settings.address);
                }
            });
            //end
        }
        });
        function _focusByAddress(map, address) {
            var geocoder = new google.maps.ClientGeocoder();
            geocoder.getLatLng(address, function(point) {
                if (!point) alert("不能解析: " + address);
                else
                    _focus(map, point);
            });
        }

        function _focus(map, point) {

            map.setCenter(point, settings.zoom);
            settings.onSetPoint(point.toUrlValue());
            var marker = new google.maps.Marker(point, { draggable: settings.model == 'mark' });
            if (settings.model == 'mark') {
                GEvent.addListener(marker, "dragend", function() {
                    marker.closeInfoWindow();
                    settings.onSetPoint(marker.getLatLng().toUrlValue());
                });
                map.clearOverlays();
                map.addOverlay(marker);
                marker.openInfoWindowHtml('拖拽我到您要标注的位置..');
            }
            else if (settings.model == 'show') {
                map.clearOverlays();
                map.addOverlay(marker);
                marker.openInfoWindowHtml(settings.address);
            }
        }
    };
})(jQuery);